package com.store.app.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.FunctionHighActivity;
import com.store.app.LocationApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.http.Stores;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.SettingData;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/6/5.
 * 绑定淘宝账号
 */

public class BindTaoBaoAccountActivity extends BaseActivity implements IOnHttpListener {

    private ImageView public_ll_return;
    private TextView tvTitle;
    private TextView tvRight;

    private EditText et_taobao_account;//淘宝会员名
    private LinearLayout submit;//确认

    private NetRequest netRequest;
    private String mStartActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
        initData();

    }

    private void initView() {
        setContentView(R.layout.activity_bind_taobao_account);
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("绑定淘宝会员名");

        et_taobao_account = (EditText) findViewById(R.id.et_taobao_account);
        submit = (LinearLayout) findViewById(R.id.submit);
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submit();
            }
        });

        //初始化标题栏最右边的帮助控件
        tvRight = (TextView) findViewById(R.id.public_tv_right);
        tvRight.setBackgroundResource(R.drawable.super_return_help);
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BindTaoBaoAccountActivity.this,WebViewMoreActivity.class);
                intent.putExtra("url","http://help.meitianhui.com/?id=1009");
                startActivity(intent);
            }
        });
    }

    private void initListener() {

    }

    private void initData() {
        netRequest = new NetRequest(this);

        mStartActivity = getIntent().getStringExtra("startActivity");
    }


    private Dialog dialog;

    private void submit() {
        final String account = et_taobao_account.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            Toast.makeText(this, "请输入淘宝会员名!", Toast.LENGTH_SHORT).show();
            return;
        }


        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.create();
        LayoutInflater inflater = LayoutInflater.from(this);
        View temp = inflater.inflate(R.layout.dialog_bind_taobao, null);
        TextView tv_taobao_account = (TextView) temp.findViewById(R.id.tv_taobao_account);
        tv_taobao_account.setText(account);
        TextView tv_ok = (TextView) temp.findViewById(R.id.tv_ok);
        TextView tv_cancel = (TextView) temp.findViewById(R.id.tv_cancel);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();

                if(mStartActivity.equals(SetActivity.class.getSimpleName())){
                    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(BindTaoBaoAccountActivity.this);
                    String login_phone = preferences.getString("login_phone", "");
                    if(!TextUtils.isEmpty(LocationApplication.userId) && !TextUtils.isEmpty(login_phone) && !TextUtils.isEmpty(MainActivity.member_id)){
                        netRequest.requestBindTaoBao(1, LocationApplication.userId, login_phone, SettingData.STORES, MainActivity.member_id,"taobao",account);
                    }
                }else if(mStartActivity.equals(EditTaoBaoAccountActivity.class.getSimpleName())){
                    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(BindTaoBaoAccountActivity.this);
                    String account_id = preferences.getString("account_id", "");
                    String login_phone = preferences.getString("login_phone", "");
                    if(!TextUtils.isEmpty(account_id) && !TextUtils.isEmpty(LocationApplication.userId) && !TextUtils.isEmpty(login_phone)
                            && !TextUtils.isEmpty(MainActivity.member_id)){
                        netRequest.requestEditTaoBao(2,account_id,"taobao",account, LocationApplication.userId,login_phone, MainActivity.member_id, SettingData.STORES);
                    }
                }


            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialogBuilder.setView(temp);
        dialog = dialogBuilder.show();
    }

    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络!");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {

        if(requestId == 1){
            Log.d("xgp", "bindSuccess:" + result);
            try {
                JSONObject jsonObject = new JSONObject(result);
                String account_id = jsonObject.getString("account_id");
                String account_no = jsonObject.getString("account_no");
                //绑定成功后，将数据存储到SP文件中
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("account_id",account_id).apply();
                editor.putString("account_no",account_no).apply();
                showToast("绑定淘宝会员名成功");
                Intent intent = new Intent();
                intent.putExtra("account_no",account_no);
                setResult(Activity.RESULT_OK,intent);
                finish();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else if(requestId == 2){
            Log.d("xgp", "modifySuccess:" + result);
            //修改成功后，将数据存储到SP文件中
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("account_no",et_taobao_account.getText().toString().trim()).apply();
            showToast("绑定淘宝会员名成功");
            enterSetActivity();
        }

    }

    /**
     * 进入设置界面
     */
    private void enterSetActivity() {
        Intent intent = new Intent(this,SetActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        showToast(msg);
    }

}
